var express = require('express');
var router = express.Router();
var xml2js = require('xml2js');

const testUrl = 'http://106.14.212.146:8288/quandiExpressSiteManager/wechatuser?pagination=1&rownum=10'

var builder = new xml2js.Builder({ rootName: 'root', attrkey: '@', charkey: '#text' });  // JSON->xml
var parser = new xml2js.Parser({ explicitArray: false, attrkey: '@', charkey: '#text' });   //xml -> json

const parseString = (xml) => {
  return new Promise((resolve) => {
    parser.parseString(xml, (_, res) => {
      resolve(res)
    })
  })
}

/* GET users listing. */
router.all('/json2xml', function (req, res, next) {
  const data = req.body
  console.log('data', data)
  var xml = builder.buildObject(data)
  res.send(xml);
});

router.all('/xml2json', async function (req, res, next) {
  const { xml } = req.body
  const data = await parseString(xml)
  res.send({
    code: 200,
    data
  })
});

router.all('/uspsinfo', async function (req, res) {
  console.log('ss')
  res.send({
    testUrl
  })
})

module.exports = router;
